package com.idealighter.game.server.db.datadb.dao;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.constant.group.GroupRoleConstant;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupMemberDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupMemberDomainExample;
import com.idealighter.game.server.db.datadb.mapper.RoomCardGroupMemberMapper;
import com.idealighter.utils.check.EmptyUtil;

import java.util.ArrayList;
import java.util.List;

@Singleton
public class RoomCardGroupMemberDao {

  private RoomCardGroupMemberMapper mapper;

  @Inject
  public RoomCardGroupMemberDao(RoomCardGroupMemberMapper mapper) {
    this.mapper = mapper;
  }


  public int insertSelective(RoomCardGroupMemberDomain domain) {
    return mapper.insertSelective(domain);
  }

  /**
   * 查找成员列表.
   *
   * @author abin
   * @date 2018年5月23日 下午4:26:05
   * @param groupId 群id.
   * @param lastId 最后id.
   * @param limit 页大小.
   * @return 结果.
   */
  public List<RoomCardGroupMemberDomain> selectByLimit(int groupId, Integer lastId, Byte role,
      int limit) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    example.setOrderByClause("role desc , id desc");
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();

    criteria.andGroupIdEqualTo(groupId);

    if (lastId != null) {
      criteria.andIdLessThan(lastId);
    }
    if (role != null) {
      criteria.andRoleEqualTo(role);
    }
    example.setLimit(limit);

    List<RoomCardGroupMemberDomain> allMembers = new ArrayList<>();

    List<RoomCardGroupMemberDomain> memberDomains = mapper.selectByExample(example);

    int lackLimit = limit;
    if (EmptyUtil.listIsNotEmpty(memberDomains)) {
      lackLimit -= memberDomains.size();
      allMembers.addAll(memberDomains);
    }

    if (lackLimit > 0 && role.byteValue() > GroupRoleConstant.NORMAL) {
      example.clear();
      example.setOrderByClause("role desc , id desc");
      criteria = example.createCriteria();

      criteria.andGroupIdEqualTo(groupId);

      criteria.andRoleLessThan(role);
      example.setLimit(lackLimit);

      List<RoomCardGroupMemberDomain> lackMemberDomains = mapper.selectByExample(example);

      if (EmptyUtil.listIsNotEmpty(lackMemberDomains)) {
        allMembers.addAll(lackMemberDomains);
      }
    }
    return allMembers;

  }

  public List<Integer> selectMyGroup(long playerId, Integer groupId, int limit) {
    return mapper.findMyGroup(playerId, groupId, limit);
  }

  /**
   * 统计我创建的群 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:26:32
   * @param playerId 玩家id.
   * @return 创建几个群.
   */
  public long countMyCreateRoom(long playerId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andPlayerIdEqualTo(playerId).andRoleEqualTo(GroupRoleConstant.OWNER);

    return mapper.countByExample(example);
  }

  /**
   * 统计我加入的群 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:26:54
   * @param playerId 玩家id.
   * @return 加入几个群.
   */
  public long countMyJoinRoom(long playerId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andPlayerIdEqualTo(playerId);

    return mapper.countByExample(example);
  }


  public List<String> selectTop9Avatar(Integer groupId) {
    return mapper.selectTop9Avatar(groupId);
  }

  /**
   * 是管理员或者群主 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:27:19
   * @param groupId 群id.
   * @param playerId 玩家id.
   * @return 结果.
   */
  public boolean isAdminOrOwner(Integer groupId, Long playerId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andPlayerIdEqualTo(playerId)
        .andRoleNotEqualTo(GroupRoleConstant.NORMAL);
    return mapper.countByExample(example) > 0;
  }

  /**
   * 获取我的群角色信息 .
   *
   * @author abin
   * @date 2018年5月23日 下午8:08:14
   * @param groupId 群id.
   * @param playerId 用户id.
   * @return 群信息.
   */
  public RoomCardGroupMemberDomain selectMyRole(Integer groupId, Long playerId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andPlayerIdEqualTo(playerId);

    RoomCardGroupMemberDomain result = null;
    List<RoomCardGroupMemberDomain> roomCardGroupMemberDomains = mapper.selectByExample(example);

    if (EmptyUtil.listIsNotEmpty(roomCardGroupMemberDomains)) {
      result = roomCardGroupMemberDomains.get(0);
    }
    return result;
  }

  /**
   * 是否群主 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:27:53
   * @param groupId 群id.
   * @param playerId 玩家id.
   * @return 结果.
   */
  public boolean isOwner(Integer groupId, Long playerId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andPlayerIdEqualTo(playerId)
        .andRoleEqualTo(GroupRoleConstant.OWNER);
    return mapper.countByExample(example) > 0;
  }

  /**
   * 是否群成员 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:28:14
   * @param groupId 群id.
   * @param playerId 玩家id.
   * @return 结果.
   */
  public boolean isMember(Integer groupId, Long playerId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andPlayerIdEqualTo(playerId);
    return mapper.countByExample(example) > 0;
  }


  /**
   * 统计管理员人数 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:28:33
   * @param groupId 群id.
   * @return 管理员人数.
   */
  public long countAdmin(Integer groupId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andRoleEqualTo(GroupRoleConstant.ADMIN);
    return mapper.countByExample(example);
  }

  /**
   * 删除所有群成员 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:28:54
   * @param groupId 群id.
   * @return 影响条数.
   */
  public int deleteAllByGroupId(int groupId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId);

    return mapper.deleteByExample(example);
  }


  /**
   * 修改群角色 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:29:17
   * @param groupId 群id.
   * @param playerId 玩家id.
   * @param role 角色.
   * @return 影响条数.
   */
  public int changeRole(Integer groupId, Long playerId, byte role) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andPlayerIdEqualTo(playerId);

    RoomCardGroupMemberDomain update = new RoomCardGroupMemberDomain();
    update.setRole(role);

    return mapper.updateByExampleSelective(update, example);
  }


  /**
   * 删除群成员 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:29:41
   * @param groupId 群id.
   * @param toPlayerId 玩家id.
   * @return 影响条数.
   */
  public int delete(Integer groupId, Long toPlayerId) {
    RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
    RoomCardGroupMemberDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andPlayerIdEqualTo(toPlayerId);

    return mapper.deleteByExample(example);
  }


}
